package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.guigu.erp.dto.StorageGatherDto;
import com.guigu.erp.mapper.StorageGatherDetailsMapper;
import com.guigu.erp.mapper.StorageGatherMapper;

import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.ManufactureDesignProcedure;
import com.guigu.erp.pojo.StorageGather;
import com.guigu.erp.pojo.StorageGatherDetails;
import com.guigu.erp.service.StorageGatherService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.StorageGatherTwoVo;
import com.guigu.erp.vo.StorageGatherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:StorageGatherMapperServiceImpl
 * Package:com.guigu.erp.service.impl
 * Description:
 *
 * @Date:2024/3/19 18:58
 * @Author:chenchong
 */

@Service
public class StorageGatherServiceImpl extends ServiceImpl<StorageGatherMapper, StorageGather>
implements StorageGatherService {
    @Autowired
    private StorageGatherMapper storageGatherMapper;
    @Autowired
    private StorageGatherDetailsMapper storageGatherDetailsMapper;
    @Override
    @Transactional
    public void page1(StorageGatherDto dto) {
        this.save(dto);
        List<StorageGatherDetails> details = dto.getDetails();
        for(int i=0;i<details.size();i++){
            StorageGatherDetails storageGatherDetails = details.get(i);
            //每条id明细设置为null
            storageGatherDetails.setId(null);
            storageGatherDetails.setParentId(dto.getId());
            storageGatherDetails.setGatherTag("K002-1");
            BigDecimal num=new BigDecimal(0);
            storageGatherDetails.setGatheredAmount(num);
            storageGatherDetailsMapper.insert(storageGatherDetails);
        }

    }

    @Override
    public String getMaxprocedure() {
        StorageGather designModule = storageGatherMapper.queryMaxModule();
        if(designModule==null){
            return null;
        }
        String designId = designModule.getGatherId();
        return designId;

    }

    @Override
    public PageUtils<StorageGather> updateAudit() {
        QueryWrapper<StorageGather> qw=new QueryWrapper<>();

        qw.eq("CHECK_TAG", "S001-0");

        List<StorageGather> list = storageGatherMapper.selectList(qw);
        PageInfo<StorageGather> p=new PageInfo<>(list);
        PageUtils<StorageGather> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;

    }

    @Override
    public PageUtils<StorageGather> show(StorageGatherVo vo,int pageNo, int pageSize) {
        QueryWrapper<StorageGather> qw = new QueryWrapper<>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getGatherId())) {
                qw.eq("GATHER_ID", vo.getGatherId());
            }
            if (!StringUtils.isEmpty(vo.getCheckTag())) {
                qw.eq("CHECK_TAG", vo.getCheckTag());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<StorageGather> list = storageGatherMapper.selectList(qw);
        PageInfo<StorageGather> p=new PageInfo<>(list);
        PageUtils<StorageGather> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @Override
    public PageUtils<StorageGather> twoShow(StorageGatherTwoVo vo, int pageNo, int pageSize) {
       QueryWrapper<StorageGather> qw=new QueryWrapper<>();
        qw.eq("CHECK_TAG","S001-1");
        qw.eq("STORE_TAG","K002-1");
       if(vo!=null){
           if(!StringUtils.isEmpty(vo.getReason())){
               qw.eq("REASON",vo.getReason());
           }
           if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
               qw.ge("register_time", vo.getSearch_registerTime1());
           }
           if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
               qw.le("register_time", vo.getSearch_registerTime2());
           }
       }
        PageHelper.startPage(pageNo, pageSize);
        List<StorageGather> list = storageGatherMapper.selectList(qw);
        PageInfo<StorageGather> p=new PageInfo<>(list);
        PageUtils<StorageGather> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
}
